package com.ruoyi.web.controller.market;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.MarketCustomer;
import com.ruoyi.market.service.MarketCustomerService;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.domain.SysProvince;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.SysCityService;
import com.ruoyi.system.service.SysProvinceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 市场 controller 层
 */
@Controller
@RequestMapping("/market/customer")
public class MarketController extends BaseController {

    @Autowired
    private MarketCustomerService customerService;

    // 品牌管理 左侧菜单栏
    @RequiresPermissions("market:customer:view")
    @GetMapping()
    public String Customer()
    {
        return "market/customer";
    }

    @RequiresPermissions("market:customer:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(MarketCustomer marketCustomer) {
        startPage();
        List<MarketCustomer> list = customerService.qureyCustomerList(marketCustomer);
        return getDataTable(list);
    }

    // 查询详细方法
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, Model model)
    {
        MarketCustomer byId = customerService.getById(id);
        System.out.println(byId);
        model.addAttribute("ids", byId);
        return "market/detail";
    }

    /**
     * 客户信息  跳转到添加页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser user = getSysUser();//当前登录用户信息
        mmap.put("user",user);
        return "market/add";
    }

    /**
     * 新增保存客户信息
     */
    @RequiresPermissions("market:customer:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping("/doadd")
    @ResponseBody
    public AjaxResult addSave(MarketCustomer customer) {
        return toAjax(customerService.save(customer));
    }


    /**
     * 修改客户信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model)
    {
        MarketCustomer byId = customerService.getById(id);
        System.out.println(byId);
        model.addAttribute("sysStudent", byId);
        return "/market/edit";
    }

    /**
     * 修改保存客户信息
     */
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/doedit")
    @ResponseBody
    public AjaxResult editSave(@Validated MarketCustomer byId)
    {
        return toAjax(customerService.updateById(byId));
    }

    /**
     * 删除客户信息
     */
    @RequiresPermissions("market:customer:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(customerService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 导出客户信息列表
     */
    @RequiresPermissions("market:customer:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MarketCustomer marketCustomer)
    {
        List<MarketCustomer> list = customerService.qureyCustomerList(marketCustomer);
        ExcelUtil<MarketCustomer> util = new ExcelUtil<MarketCustomer>(MarketCustomer.class);
        return util.exportExcel(list, "marketCustomer");
    }
}
